/*
* @Author: Administrator
* @Date:   2017-07-21 14:00:30
* @Last Modified by:   Administrator
* @Last Modified time: 2017-11-16 10:39:57
*/

'use strict';
var webpack = require('webpack');
var path    = require('path');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
//环境变量
var WEBPACK_ENV = process.env.WEBPACK_ENV||'dev';
//webpack config
var config = {

    entry : {
        'app' : ['./src/index.jsx']
    },
    // path && publickPath
    output :{
        path : __dirname+'/dist/',
        publicPath  : '//s.shop.jhmaimai.cn/admin-jhmm/dist/',
        //publicPath : '/dist/',
        filename : 'js/[name].js'
    },
    resolve : {
        alias:{
            node_modules    : path.join(__dirname, '/node_modules'),
            lib             : path.join(__dirname, '/src/lib'),
            util            : path.join(__dirname, '/src/util'),
            component       : path.join(__dirname, '/src/component'),
            service         : path.join(__dirname, '/src/service'),
            page            : path.join(__dirname, '/src/page')
        }
    },
    module : {
        // noParse: [],
        loaders: [
            {
                test: /\.css$/, loader: ExtractTextPlugin.extract({
                    use: 'css-loader',
                    fallback : 'style-loader'
                })
            },
            {test: /\.scss$/, loader: ExtractTextPlugin.extract({
                use: 'css-loader!sass-loader',
                fallback : 'style-loader'
            })},
            {test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=20000&name=resource/[name].[ext]'},
            {test: /\.(string)$/, loader: 'html-loader' },
            {
                test: /\.js?$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.jsx?$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'es2015']
                }
            },
        ]
    },
    externals : {
        '$' : 'window.jQuery',
        'jquery' : 'window.jQuery'
    },
    plugins : [
        //提取公共部分
        new webpack.optimize.CommonsChunkPlugin({
            name:'vendors',
            filename:'js/base.js'
        }),
        new webpack.optimize.UglifyJsPlugin({
              output: {
                comments: false,  // remove all comments
              },
              compress: {
                warnings: false
              }
        }),
        new webpack.DefinePlugin({
             'process.env': {
                WEBPACK_ENV: JSON.stringify(process.env.WEBPACK_ENV),
             },
        }),
        //单独处理css
        new ExtractTextPlugin("css/[name].css"),
        //处理html
        new HtmlWebpackPlugin({
            filename        : 'view/index.html',
            title           : 'JHMM 后台管理系统',
            template        : './src/index.html',
            favicon         : './favicon.ico',
            inject          : true,
            hash            : true,
            chunks          : ['vendors', 'app'],
            chunksSortMode  : 'dependency',
            minify          : {
                removeComments: true,
                collapseWhitespace: false
            }
        }),
    ]

}
if(WEBPACK_ENV === 'dev'){
    config.entry.app.push('webpack-dev-server/client?http://localhost:8086');
}

module.exports = config;